package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description NusingProjectServiceImpl
 * @Author LiShunSen
 * @Date 2024-08-24
 */
@Slf4j
@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 分页查询
     *
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageResponse<NursingProjectVo> getByPage(
            String name,
            Integer pageNum,
            Integer pageSize,
            Integer status) {
        //启动分页查询
        PageHelper.startPage(pageNum, pageSize);
        //查询
        Page<NursingProjectVo> page = nursingProjectMapper.getByPage(name, status);
        //封装分页结果
        PageResponse<NursingProjectVo> pageResponse = new PageResponse<>(page);
        pageResponse.setRecords(page.getResult());
        return pageResponse;
    }

    /**
     * 新增护理项目
     *
     * @param nursingProjectDto
     * @return
     */
    public void addProjec(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProject.setCreateTime(LocalDateTime.now());
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProjectMapper.inviteProject(nursingProject);
    }

    @Override
    public NursingProjectVo getById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.getById(id);
        return BeanUtil.toBean(nursingProject, NursingProjectVo.class);
    }

    @Override
    public void updateProject(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProject.setUpdateBy(10L);
        nursingProjectMapper.updateProject(nursingProject);
    }

    @Override
    public void statusProject(Long id, Integer status) {
        nursingProjectMapper.statusProjectById(id, status);
    }

    @Override
    public void deletProJect(Long id) {
        nursingProjectMapper.deletProJect(id);
    }

    @Override
    public List<NursingProjectVo> findAll() {
        List<NursingProjectVo>  nursingProjectVo = nursingProjectMapper.findAll();
      return nursingProjectVo;
    }
}
